package com.lianniu.web.tea.token.controller;

import java.io.UnsupportedEncodingException;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianniu.web.tea.token.model.JsapiTicket;
import com.lianniu.web.tea.token.model.TeaAccessToken;
import com.lianniu.web.tea.token.service.TeaAccessTokenService;
import com.lianniu.web.tea.token.util.WeixinUtil;

@Controller
@RequestMapping("/rest/tokenValidate")
public class TokenValidateController {

	private String TOKEN = "tokenChat";

	private static final String appId = "wxb0f5fe89c5840bf4";
	// 第三方用户唯一凭证密钥
	private static final String appSecret = "f5d9e0b78ca03c83d72323eded7b3876";

	
	private final static Logger logger = Logger.getLogger(TeaAccessTokenService.class);

	@Autowired
	private TeaAccessTokenService teaAccessTokenService;

	/**
	 * 请求获取到测试用到的access_token
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getAccessToken", method = RequestMethod.GET)
	public @ResponseBody String getAccessToken() {
		logger.info("TokenValidateController getAccessToken start");
		String accessToken = "";
		try {
			accessToken = teaAccessTokenService.getAccessToken();
		} catch (Exception e) {
			logger.info("TokenValidateController getAccessToken error:" + e.getMessage());
		}
		logger.info("TokenValidateController getAccessToken end");
		return accessToken;
	}

	@RequestMapping(value = "/getSignature", method = { RequestMethod.POST, RequestMethod.GET })
	public @ResponseBody JsapiTicket getSignature(@RequestParam(value = "url") String url)
			throws UnsupportedEncodingException {
		/*String s = new String(url.getBytes("ISO8859-1"), "UTF-8");
		System.err.println(s);*/
		String jsapiTicket = teaAccessTokenService.getJsapiTicket();
		JsapiTicket sign = WeixinUtil.sign(jsapiTicket, url);
		System.err.println(url);
		return sign;
	}

	@RequestMapping(value = "/getJsapiTicket", method = RequestMethod.GET)
	public @ResponseBody String getJsapiTicket() {
		logger.info("TokenValidateController getJsapiTicket start");
		String jsapiTicket = "";
		try {
			jsapiTicket = teaAccessTokenService.getJsapiTicket();
		} catch (Exception e) {
			logger.info("TokenValidateController getJsapiTicket error:" + e.getMessage());
		}
		logger.info("TokenValidateController getJsapiTicket end");
		return jsapiTicket;
	}
	
	@RequestMapping(value = "/creatAccessToken", method = RequestMethod.GET)
	public @ResponseBody String creatAccessToken(){
		TeaAccessToken accessToken = WeixinUtil.getAccessToken(appId, appSecret);
		TeaAccessToken jsapiTicket = WeixinUtil.getJsapiTicket(accessToken.getAccessTocken());
		logger.info("accessToken:"+accessToken.getAccessTocken());
		teaAccessTokenService.saveToken(accessToken,jsapiTicket);
		String token = teaAccessTokenService.getAccessToken();
		return token;
		
	}

}
